from selenium.webdriver.common.by import By
from base.mis.base import BasePage, BaseHandle
from utils import UtilsDriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException


# 定义对象库层
class LoginPage(BasePage):
    def __init__(self):
        super().__init__()
        self.username = By.CSS_SELECTOR, "input[placeholder='请输入手机号']"
        self.password = By.CSS_SELECTOR, "input[placeholder='验证码']"
        self.login_btn = By.CSS_SELECTOR, "button.el-button.el-button--primary"

    def find_username(self):
        # 等待 loading 遮罩层消失
        WebDriverWait(self.driver, 10).until(
            EC.invisibility_of_element_located((By.CSS_SELECTOR, ".el-loading-mask"))
        )
        ele = self.get_element(self.username)
        WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(self.username))
        ele.clear()
        ele.send_keys(Keys.CONTROL + "a")
        ele.send_keys(Keys.DELETE)
        print("username input value (after clear):", ele.get_attribute("value"))
        return ele

    # 定位密码输入框
    def find_password(self):
        ele = self.get_element(self.password)
        WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(self.password))
        ele.clear()
        ele.send_keys(Keys.CONTROL + "a")
        ele.send_keys(Keys.DELETE)
        print("password input value (after clear):", ele.get_attribute("value"))
        return ele

    # 定位"登录"按钮
    def find_login_btn(self):
        return self.get_element(self.login_btn)

    def click_login_btn(self):
        """点击登录按钮"""
        # 等待登录按钮可点击
        WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(self.login_btn))
        ele = self.get_element(self.login_btn)
        print("Login button is clickable")
        # 使用普通的 Selenium 点击方法
        ele.click()
        print("Login button clicked")
        # 等待登录完成（等待加载遮罩消失）
        try:
            WebDriverWait(self.driver, 10).until(
                EC.invisibility_of_element_located((By.CSS_SELECTOR, ".el-loading-mask"))
            )
            print("Loading mask disappeared")
        except TimeoutException:
            print("Loading mask did not disappear within timeout")


# 定义操作层
class LoginHandle(BaseHandle):
    def __init__(self):
        self.login_page = LoginPage()

    def input_username(self, username):
        self.input_text(self.login_page.find_username(), username)

    def input_password(self, password):
        self.input_text(self.login_page.find_password(), password)

    def click_login_btn(self):
        self.login_page.click_login_btn()


class LoginProxy:
    def __init__(self):
        self.login_handle = LoginHandle()

    def login(self, username, password):
        self.login_handle.input_username(username)
        self.login_handle.input_password(password)
        self.login_handle.click_login_btn()
